package com.awcall.iot.common;

import com.awcall.iot.model.Authority;
import com.awcall.iot.model.RoleAuthority;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Tree {

    private Integer id;
    private String title;
    private String path;
    private String component;
    private String name;
    private Boolean checked;
    private List<Tree> children;
    private List<Integer> access;

    public Boolean getChecked() {
        return checked;
    }

    public Tree setChecked(Boolean checked) {
        this.checked = checked;
        return this;
    }

    public String getPath() {
        return path;
    }

    public Tree setPath(String path) {
        this.path = path;
        return this;
    }

    public String getComponent() {
        return component;
    }

    public Tree setComponent(String component) {
        this.component = component;
        return this;
    }

    public String getName() {
        return name;
    }

    public Tree setName(String name) {
        this.name = name;
        return this;
    }

    public List<Integer> getAccess() {
        return access;
    }

    public Tree setAccess(List<Integer> access) {
        this.access = access;
        return this;
    }

    public Tree(Authority authority) {
        this.id = authority.getId();
        this.name = authority.getAuthorityName();
        this.title = authority.getTitle();
        this.component = authority.getComponent();
        this.path = authority.getPath();
        this.checked = authority.getChecked();
        if (!CollectionUtils.isEmpty(authority.getRoleAuthorityList())) {
            this.access = authority.getRoleAuthorityList()
                    .stream()
                    .map(RoleAuthority::getRoleId)
                    .collect(Collectors.toList());
        }
    }

    public void addChildren(Tree tree) {
        if (CollectionUtils.isEmpty(children)) {
            this.children = Stream.of(tree).collect(Collectors.toList());
        } else {
            this.children.add(tree);
        }
    }

    public Integer getId() {
        return id;
    }

    public Tree setId(Integer id) {
        this.id = id;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public Tree setTitle(String title) {
        this.title = title;
        return this;
    }

    public List<Tree> getChildren() {
        return children;
    }

    public Tree setChildren(List<Tree> children) {
        this.children = children;
        return this;
    }
}
